﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Common.Utilities;
using Common.Utilities.Dispatcher;

namespace Workflows.Components.TextMining.Ontologies
{
    /// <summary>
    /// 
    /// </summary>
    public partial class WordFreqEditor : Form, IComponentEditor
    {
        /// <summary>
        /// 
        /// </summary>
        public WordFreqEditor()
        {
            InitializeComponent();
        }

        private void WordFreqEditor_Load(object sender, EventArgs e)
        {

        }

        #region Implementation of IComponentEditor

        private object _ActivityNode;
        private WordFreqComponent _WordFreq;
        /// <summary>
        /// 
        /// </summary>
        public event UpdateComponentSettings UpdateComponentSettingEvent;

        /// <summary></summary>
        public object OwnerActivityNode
        {
            get { return _ActivityNode; }
            set { _ActivityNode = value; }
        }

        /// <summary></summary>
        public Type WorkflowComponentType
        {
            get { return typeof(WordFreqComponent); }
        }

        /// <summary></summary>
        public void LoadSettings(GlobalVariable[] globalVars, WorkflowComponentBase[] fromComponents, object activityNode, WorkflowComponentBase wfComponent)
        {
            this._ActivityNode = activityNode;
            this._WordFreq = (WordFreqComponent) wfComponent;

            WordFreqInstruction instruction = (WordFreqInstruction) this._WordFreq.Instruction;
            this.txt_SrcConn.Text = instruction.ConnStr;
            this.rt_SQL.Text = instruction.SQL;
            this.PopulateSrcFields(instruction.ConnStr, instruction.SQL);
            this.SelectComboxItem(this.cbo_DocID, instruction.DocIDField);
            this.SelectComboxItem(this.cbo_TextField, instruction.TextField);
            this.SelectComboxItem(this.cbo_YearField, instruction.YearField);

            this.txt_TgtConn.Text = instruction.OutputConnStr;
            this.PopulateTargetTable(instruction.OutputConnStr);
            this.SelectComboxItem(this.cbo_TermsTable, instruction.TermTable);
            this.SelectComboxItem(this.cbo_DocTermTable, instruction.DocTermTable);
            this.SelectComboxItem(this.cbo_NeighborsTable,instruction.NeighborTable);
            this.SelectComboxItem(this.cbo_TrendsTable, instruction.TrendTable);

            this.SelectComboxItem(this.cbo_TermID, instruction.TermIDField);
            this.SelectComboxItem(this.cbo_Phrase, instruction.PhraseField);
            this.SelectComboxItem(this.cbo_RootTerm, instruction.RootTermIDField);

            this.SelectComboxItem(this.cbo_TermID2,instruction.TermIDField2);
            this.SelectComboxItem(this.cbo_DocID2,instruction.DocIDField2);
            this.SelectComboxItem(this.cbo_TermFreq,instruction.TermFreqField);

            this.SelectComboxItem(this.cbo_LeftTerm,instruction.LeftTermIDField);
            this.SelectComboxItem(this.cbo_RightTerm,instruction.RightTermIDField);
            this.SelectComboxItem(this.cbo_NeighborOcc,instruction.NeighborTotalOccuranceField);
            this.SelectComboxItem(this.cbo_DocFreq2, instruction.DocContainingNeighborField);

            this.SelectComboxItem(this.cbo_TermID3, instruction.TermIDField3);
            this.SelectComboxItem(this.cbo_TermYear,instruction.TermYearField);
            this.SelectComboxItem(this.cbo_SumTermFreq,instruction.SumTermFreqField);
            this.SelectComboxItem(this.cbo_SumDocFreq,instruction.SumDocFreqField);
        }

        /// <summary></summary>
        public void SaveSettings()
        {
            WordFreqInstruction instruction = (WordFreqInstruction) this._WordFreq.Instruction;
            instruction.ConnStr = this.txt_SrcConn.Text;
            instruction.SQL = this.rt_SQL.Text;
            instruction.DocIDField = this.cbo_DocID.Text;
            instruction.TextField = this.cbo_TextField.Text;
            instruction.YearField = this.cbo_YearField.Text;

            instruction.OutputConnStr = this.txt_TgtConn.Text;
            instruction.TermTable = this.cbo_TermsTable.Text;
            instruction.TermIDField = this.cbo_TermID.Text;
            instruction.PhraseField = this.cbo_Phrase.Text;
            instruction.RootTermIDField = this.cbo_RootTerm.Text;

            instruction.DocTermTable = this.cbo_DocTermTable.Text;
            instruction.TermIDField2 = this.cbo_TermID2.Text;
            instruction.DocIDField2 = this.cbo_DocID2.Text;
            instruction.TermFreqField = this.cbo_TermFreq.Text;

            instruction.NeighborTable = this.cbo_NeighborsTable.Text;
            instruction.LeftTermIDField = this.cbo_LeftTerm.Text;
            instruction.RightTermIDField = this.cbo_RightTerm.Text;
            instruction.NeighborTotalOccuranceField = this.cbo_NeighborOcc.Text;
            instruction.DocContainingNeighborField = this.cbo_DocFreq2.Text;

            instruction.TrendTable = this.cbo_TrendsTable.Text;
            instruction.TermIDField3 = this.cbo_TermID3.Text;
            instruction.TermYearField = this.cbo_TermYear.Text;
            instruction.SumTermFreqField = this.cbo_SumTermFreq.Text;
            instruction.SumDocFreqField = this.cbo_SumDocFreq.Text;
        }

        /// <summary></summary>
        public TestResult Test()
        {
            throw new NotImplementedException();
        }

        #endregion

        #region private
        private void PopulateSrcFields(string connStr, string sql)
        {
            this.cbo_DocID.Items.Clear();
            this.cbo_TextField.Items.Clear();
            this.cbo_YearField.Items.Clear();
            if(!string.IsNullOrEmpty(connStr) && !string.IsNullOrEmpty(sql))
            {
                TableColumn[] cols = DbUtil.GetTableColumns(
                    DataProviderType.MSSQL, connStr, sql);
                if(cols !=null && cols.Length>0)
                {
                    foreach(TableColumn col in cols)
                    {
                        this.cbo_DocID.Items.Add(col.ColumnName);
                        this.cbo_TextField.Items.Add(col.ColumnName);
                        this.cbo_YearField.Items.Add(col.ColumnName);
                    }
                }
            }
        }

        private void PopulateTargetTable(string connStr)
        {
            this.cbo_TermsTable.Items.Clear();
            this.cbo_DocTermTable.Items.Clear();
            this.cbo_NeighborsTable.Items.Clear();
            this.cbo_TrendsTable.Items.Clear();

            if(!string.IsNullOrEmpty(connStr))
            {
                string[] tblNames = DbUtil.GetTableNames(DataProviderType.MSSQL, "dbo", connStr);
                if(tblNames !=null)
                {
                    foreach (string tblName in tblNames)
                    {
                        this.cbo_TermsTable.Items.Add(tblName);
                        this.cbo_DocTermTable.Items.Add(tblName);
                        this.cbo_NeighborsTable.Items.Add(tblName);
                        this.cbo_TrendsTable.Items.Add(tblName);
                    }
                }
            }
        }

        private void PopulateTgtFields(ComboBox comboBox, string connStr, string tblName)
        {
            comboBox.Items.Clear();
            if(!string.IsNullOrEmpty(connStr) && !string.IsNullOrEmpty(tblName))
            {
                TableColumn[] cols = DbUtil.GetTableColumns(
                    DataProviderType.MSSQL,
                    connStr, "dbo", tblName);
                if(cols !=null && cols.Length>0)
                {
                    foreach(TableColumn col in cols)
                    {
                        comboBox.Items.Add(col.ColumnName);
                    }
                }
            }
        }

        private void SelectComboxItem(ComboBox comboBox, string itemValue)
        {
            if(!string.IsNullOrEmpty(itemValue) && comboBox.Items.Count>0)
            {
                for(int i=0;i<comboBox.Items.Count;i++)
                {
                    if(comboBox.Items[i].ToString()==itemValue)
                    {
                        comboBox.SelectedIndex = i;
                        break;
                    }
                }
            }
        }
        #endregion

        private void cbo_TermTable_SelectedIndexChanged(object sender, EventArgs e)
        {
            if(this.cbo_TermsTable.SelectedIndex>=0)
            {
                this.PopulateTgtFields(this.cbo_TermID, this.txt_TgtConn.Text,this.cbo_TermsTable.SelectedItem.ToString());
                this.PopulateTgtFields(this.cbo_RootTerm, this.txt_TgtConn.Text, this.cbo_TermsTable.SelectedItem.ToString());
                this.PopulateTgtFields(this.cbo_Phrase, this.txt_TgtConn.Text, this.cbo_TermsTable.SelectedItem.ToString());
            }
        }

        private void btn_Cancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btn_OK_Click(object sender, EventArgs e)
        {
            this.SaveSettings();
            if (this.UpdateComponentSettingEvent != null)
                this.UpdateComponentSettingEvent(this._ActivityNode, this._WordFreq);
            this.Close();
        }

        private void cbo_DocTermTable_SelectedIndexChanged(object sender, EventArgs e)
        {
            if(this.cbo_DocTermTable.SelectedIndex>=0)
            {
                this.PopulateTgtFields(this.cbo_TermID2,this.txt_TgtConn.Text,this.cbo_DocTermTable.SelectedItem.ToString());
                this.PopulateTgtFields(this.cbo_DocID2, this.txt_TgtConn.Text, this.cbo_DocTermTable.SelectedItem.ToString());
                this.PopulateTgtFields(this.cbo_TermFreq, this.txt_TgtConn.Text, this.cbo_DocTermTable.SelectedItem.ToString());
            }
        }

        private void cbo_NeighborsTable_SelectedIndexChanged(object sender, EventArgs e)
        {
            if(this.cbo_NeighborsTable.SelectedIndex>=0)
            {
                this.PopulateTgtFields(this.cbo_LeftTerm,this.txt_TgtConn.Text,this.cbo_NeighborsTable.SelectedItem.ToString());
                this.PopulateTgtFields(this.cbo_RightTerm, this.txt_TgtConn.Text, this.cbo_NeighborsTable.SelectedItem.ToString());
                this.PopulateTgtFields(this.cbo_DocFreq2, this.txt_TgtConn.Text, this.cbo_NeighborsTable.SelectedItem.ToString());
                this.PopulateTgtFields(this.cbo_NeighborOcc, this.txt_TgtConn.Text, this.cbo_NeighborsTable.SelectedItem.ToString());
            }
        }

        private void cbo_TrendsTable_SelectedIndexChanged(object sender, EventArgs e)
        {
            if(this.cbo_TrendsTable.SelectedIndex>=0)
            {
                this.PopulateTgtFields(this.cbo_TermID3,this.txt_TgtConn.Text,this.cbo_TrendsTable.SelectedItem.ToString());
                this.PopulateTgtFields(this.cbo_TermYear, this.txt_TgtConn.Text, this.cbo_TrendsTable.SelectedItem.ToString());
                this.PopulateTgtFields(this.cbo_SumTermFreq, this.txt_TgtConn.Text, this.cbo_TrendsTable.SelectedItem.ToString());
                this.PopulateTgtFields(this.cbo_SumDocFreq, this.txt_TgtConn.Text, this.cbo_TrendsTable.SelectedItem.ToString());
            }
        }

        private void btn_ParseSQL_Click(object sender, EventArgs e)
        {
            this.PopulateSrcFields(this.txt_SrcConn.Text, this.rt_SQL.Text);
        }

        private void btn_Refresh_Click(object sender, EventArgs e)
        {
            this.PopulateTargetTable(this.txt_TgtConn.Text);
        }
    }
}
